const db = require('../config/db.js');
const inFile = require('../utils/inFile.js');
const swiperModel = {

    GetSwiperList: async () =>{
        const sql = 'select id,type,image from swiper;';
        try {
            const res = await db.query(sql);
            console.log(res[0],'获取轮播图列表调试0');
            return res[0];
        } catch (error) {
            return {
                errorMsg: error
            }
        }
    },
    AddSwiper: async ({type,image},file) => {
        console.log(type,image,'添加轮播图入参测试');
        console.log(file,'轮播图添加图片调试');
        const imgPath = inFile(file,'img/Swiper/');
        const sql ='INSERT INTO swiper (type,image) VALUES (?,?);';
        try {
            if(type&&imgPath){
                const res = await db.query(sql,[type,imgPath]);

                console.log(res[0],'添加轮播图调试');
                return res[0];
            }else{
                return;
            }
        } catch (error) {
            return {
                errorMsg: error
            }
        }
    },
    DeleteSwiper:async({id})=>{
        console.log(id,'*****');
        const sql ='DELETE FROM swiper WHERE id=?;';
        try {
            if(id){
                const res = await db.query(sql,[id]);
                console.log(res[0],'删除轮播图调试调试');
                return res[0];
            }else{
                return 0;
            }
        } catch (error) {
            return {
                errorMsg: error
            }
        }
    },
    UpdateSwiper: async ({id,type,image},file) => {
        console.log(file,'轮播修改图片调试');
        const imgPath = inFile(file,'img/Swiper/');
        const sql = 'UPDATE Swiper SET type=?,image=? WHERE id=?;';
        try {
            if(id){
                const res = await db.query(sql,[type,imgPath,id]);
                console.log(res[0],'更新轮播图调试');
                return res[0];
            }else{
                return;
            }
        } catch (error) {
            return {
                errorMsg: error
            }
        }
    },
    BatchDeleteSwiper: async  ({idList}) => {
        console.log(idList,'收集idList的数组');
        const sql = `DELETE FROM swiper WHERE id IN(${idList});`;
        try {
            if (idList.length>0){
                const res = await db.query(sql);
                console.log(res,'Model层批量删除汽车调试');
                return res[0];
            }else{
                return ;
            }
        }catch (error){
            return{
                errorMsg:error
            }
        }
    },
    //获取后台管理轮播图
    GetAdminSwiper: async ()=>{
        const sql = 'SELECT id,type,image FROM swiper WHERE type = 1;';
        try {
            const res = await db.query(sql);
            console.log(res[0],'获取后台管理轮播图列表调试0');
            return res[0];
        } catch (error) {
            return {
                errorMsg: error
            }
        }
    },
    //获取客户端轮播图
    GetClientSwiper: async  ()=>{
        const sql = 'SELECT id,type,image FROM swiper WHERE type = 2;';
        try {
            const res = await db.query(sql);
            console.log(res[0],'获取客户端轮播图列表调试0');
            return res[0];
        } catch (error) {
            return {
                errorMsg: error
            }
        }
    }
};

module.exports = swiperModel;